﻿using Microsoft.Extensions.Configuration;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Plugins.Qiniu
{
    public class QiniuHelper
    {
        
        private IConfiguration qiniuConfig;
        public QiniuHelper(IConfiguration configuration)
        {
            Configuration = configuration;
            qiniuConfig = Configuration.GetSection("Qiniu");

        }

        /// <summary>
        /// 配置属性
        /// </summary>
        public IConfiguration Configuration { get; }

        //七牛云授权参数,AK和SK个人空间查看,Bucket空间名称
        //private string AccessKey = "8-KJKs2-dqmUuqYVV31Y1QkiyprPuUYr-z6Qej46", SecretKey = "sMH-gJZDcM9LxJfMGG7UMclIsYeVhSNT61spvOFf", Bucket = "xiaoxingbobo";
        
        /// <summary>
        /// 获取qiniu授权token
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            //AccessKey,SecretKey七牛云授权
            Mac mac = new Mac(qiniuConfig.GetValue<string>("AccessKey"), qiniuConfig.GetValue<string>("SecretKey"));
            //自定义凭证有效期（示例2小时，expires单位为秒，为上传凭证的有效时间）
            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = qiniuConfig.GetValue<string>("Bucket");
            putPolicy.SetExpires(7200);
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            return token;
        }
        /// <summary>
        /// 获取七牛云列表
        /// </summary>
        /// <param name="prefix">查询前缀</param>
        /// <param name="limit">查询条数</param>
        /// <returns></returns>
        public ListResult GetList(string prefix, int limit)
        {
            // 设置存储区域
            Config config = new Config();
            config.Zone = Zone.ZONE_CN_South;
            Mac mac = new Mac(qiniuConfig.GetValue<string>("AccessKey"), qiniuConfig.GetValue<string>("SecretKey"));
            BucketManager bucketManager = new BucketManager(mac, config);
            string delimiter = "";
            // 上一次列举返回的位置标记，作为本次列举的起点信息
            string marker = "";
            ListResult listRet = bucketManager.ListFiles(qiniuConfig.GetValue<string>("Bucket"), prefix, marker, limit, delimiter);
            if (listRet.Code != (int)HttpCode.OK)
            {
                Console.WriteLine("list files error: " + listRet.ToString());
            }
            Console.WriteLine(listRet.ToString());
            return listRet;
        }
        /// <summary>
        /// 删除七牛云文件
        /// </summary>
        /// <param name="Key">删除文件名</param>
        /// <returns></returns>
        public bool Delete(string Key)
        {
            // 设置存储区域
            Config config = new Config();
            config.Zone = Zone.ZONE_CN_South;
            Mac mac = new Mac(qiniuConfig.GetValue<string>("AccessKey"), qiniuConfig.GetValue<string>("SecretKey"));
            BucketManager bucketManager = new BucketManager(mac, config);
            //执行删除
            HttpResult deleteRet = bucketManager.Delete(qiniuConfig.GetValue<string>("Bucket"), Key);
            Console.WriteLine("delete error: " + deleteRet.ToString());
            if (deleteRet.Code != (int)HttpCode.OK)
            {
                Console.WriteLine("delete error: " + deleteRet.ToString());
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

